package foundation.web.flow;

import java.util.HashMap;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericForwardComposer;

import ar.com.sne.bean.BaseBO;
import ar.com.sne.util.StringUtil;
import foundation.common.util.CommonWebUtil;

/**
 * @author Florencia Maneiro
 */
/**
 * This class implements the abstract behavior of Destination manager composer to forward to a specific windows (origin by NotificationMessage or NotificationTask)
 * 
 * 
 */
@SuppressWarnings("unchecked")
public abstract class AbstractDestinationComposer extends
		GenericForwardComposer {

	private boolean windowDestination=false;
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);			
		try{
			//Si existe alguna marca en sesion que indica que hay algun objecto para restarar
			//es decir, que viene de una notificacion o de task, se restaura y se pone en session
			Map parameters = (Map)CommonWebUtil.getParameter(CommonWebUtil.DESTINATION_PARAMETERS);
			String stringObjectId;
			String stringObjectClass;
			
			if(parameters!=null){
				stringObjectId = (String)parameters.get(CommonWebUtil.DESTINATION_OBJECT_ID);
				stringObjectClass = (String)parameters.get(CommonWebUtil.DESTINATION_OBJECT_CLASS);					
			}
			
			else{
				//tambien puede ser que venga directamente desde otra ventana, como desde compras para eso, recupero los parametros del request
				stringObjectId =Executions.getCurrent().getParameterValues(CommonWebUtil.DESTINATION_OBJECT_ID)[0];			
//				stringObjectClass=Executions.getCurrent().getParameterValues(CommonWebUtil.DESTINATION_OBJECT_CLASS)[0];
			}		
			
			if(	
//					!StringUtil.isNullOrEmpty(stringObjectClass) && 
					!StringUtil.isNullOrEmpty(stringObjectId) ){
					windowDestination=true;					
					Object object = getNewObjectPersistentObjectToAdd();
					BaseBO objectPersist =(BaseBO)object;
					objectPersist.setId(Long.valueOf(stringObjectId));
					CommonWebUtil.newFlow(Executions.getCurrent().getDesktop().getRequestPath());
					CommonWebUtil.putParameter(CommonWebUtil.OBJECT_TO_EDIT_ARGUMENT, objectPersist);
					Executions.getCurrent().pushArg(new HashMap());
					Executions.getCurrent().getArg().put(CommonWebUtil.OBJECT_TO_EDIT_ARGUMENT, objectPersist);
			}			
		}
		catch (Exception e) {
//			BusinessException be = new BusinessException();
//			be.addDescription(e.getMessage());
//			CommonGeneralUtil.throwBussinesException(be);
		}
	}
	public boolean getWindowDestination() {
		return windowDestination;
	}
	public void setWindowDestination(boolean windowDestination) {
		this.windowDestination = windowDestination;
	}
	
	

	protected abstract BaseBO getNewObjectPersistentObjectToAdd();
}
